package org.laizili.solution.leetcode;

/**
 * <a href="https://leetcode-cn.com/problems/rotate-string/">796. 旋转字符串</a>
 * <p>
 *     tags: 模拟;
 * </p>
 */
public class Problem796 {
    private static class Solution {
        public boolean rotateString(String s, String goal) {
            if (s.length() != goal.length()) {
                return false;
            }
            int strlen = s.length();
            for (int i = 0; i < strlen; i++) {
                boolean found = true;
                for (int k = 0; k < strlen; k++) {
                    if (s.charAt((k + i) % strlen) != goal.charAt(k)) {
                        found = false;
                        break;
                    }
                }
                if (found) {
                    return true;
                }
            }
            return false;
        }
    }

    // case1: s.length() != target.length() 时，false
    // case2: s+s 形成的串包含了s旋转的所有情况，因此只需搜索s+s串是否包含target子串即可
    private static class Solution2 {
        public boolean rotateString(String s, String goal) {
            return s.length() == goal.length() && (s + s).contains(goal);
        }
    }
}
